package com.lanmaoly.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * lambda表达式用法详解：http://ifeve.com/lambda/
 * stream详解：http://ifeve.com/stream/
 *
 * @author Administrator
 */
public class Test1 {

    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("TaoBao");
        names.add("ZhiFuBao");

        List<String> collect = names.stream().map((String name) -> {
            return name.toLowerCase();
        }).collect(Collectors.toList());

        // 只有一个param时 括号可以省略 大括号代码块中为逻辑 只有一行代码时可以省略
        List<String> collect1 = names.stream().map(s -> {
            return s.toLowerCase();
        }).collect(Collectors.toList());

        List<String> collect2 = names.stream().map(String::toLowerCase).collect(Collectors.toList());

        System.out.println(collect.toString());
        Stream<Double> generate = Stream.generate(new Supplier<Double>() {
            @Override
            public Double get() {
                return Math.random();
            }
        });
        Stream<Double> generate1 = Stream.generate(() -> Math.random());
        Stream<Double> generate2 = Stream.generate(Math::random);
        generate.limit(10).forEach(System.out::println);

        Stream.iterate(100, item -> item + 10).limit(10).forEach(System.out::println);

        ArrayList<String> list = new ArrayList<>(16);
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                list.add(i + "");
            } else {
                int i1 = i + 1;
                list.add(i1 + "");
            }
        }
        System.out.println(list.toString());
        List<Integer> collect3 = list.stream().map(String::hashCode).collect(Collectors.toList());
        System.out.println(collect3.toString());
//        List<Integer> ints = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
//        System.out.println("ints sum is:" + ints.stream().reduce((sum, item) -&gt; sum + item).get());
//        Optional<Integer> reduce = ints.stream().reduce((integer, integer2) -> integer + integer2);
//        reduce.ifPresent(System.out::println);
//        List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
//        List<Integer> numsWithoutNull = nums.stream().filter(Objects::nonNull).
//                collect(Collectors.toList());
//        System.out.println(numsWithoutNull.toString());
//        IntStream limit = nums.stream().filter(num -> num != null).
//                distinct().mapToInt(num -> num * 2).
//                peek(System.out::println).skip(2).limit(4);
//        limit.forEach(System.out::println);

//        List<Integer> ints1 = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10);
//        System.out.println(ints1.stream().allMatch(item -> item < 100));
//        ints1.stream().max(Integer::compareTo).ifPresent(System.out::println);

//        System.out.println(ints1.stream().anyMatch(integer -> integer == 100));
//        System.out.println(ints1.stream().noneMatch(integer -> integer == 10));
//        ints1.stream().findFirst().ifPresent(System.out::println);



    }
}
